﻿using System;
using System.Web;
using PD.DataAccess;
using PD.WorkflowLibrary.ExternalData;

namespace PD.WorkflowLibrary
{
    /// <summary>
    /// A local service that handles invoice processing.
    /// </summary>
    public class InvoiceProcessingService : IInvoiceProcessingService
    {
        /// <summary>
        /// Occurs when the invoice has been corrected.
        /// </summary>
        public event EventHandler<TaskUpdatedEventArgs> InvoiceCorrected;

        /// <summary>
        /// Occurs when the invoice has been confirmed.
        /// </summary>
        public event EventHandler<TaskUpdatedEventArgs> InvoiceConfirmed;

        /// <summary>
        /// Occurs when the invoice has been approved by chief accountant.
        /// </summary>
        public event EventHandler<TaskUpdatedEventArgs> InvoiceApprovedByChiefAccountant;

        /// <summary>
        /// Occurs when the invoice has been approved by CEO.
        /// </summary>
        public event EventHandler<TaskUpdatedEventArgs> InvoiceApprovedByCEO;

        /// <summary>
        /// Occurs when the invoice has been finally approved.
        /// </summary>
        public event EventHandler<TaskUpdatedEventArgs> InvoiceFinallyApproved;

        /// <summary>
        /// Occurs when the invoice has been archived.
        /// </summary>
        public event EventHandler<TaskUpdatedEventArgs> InvoiceArchived;

        /// <summary>
        /// Raises the invoice corrected event.
        /// </summary>
        /// <param name="taskId">The identifier of updated task.</param>
        /// <param name="instanceId">The workflow instance identifier for the workflow instance that contains the System.Workflow.Activities.HandleExternalEventActivity that is expected to handle the event.</param>
        public void RaiseInvoiceCorrected(int taskId, Guid instanceId)
        {
            if (this.InvoiceCorrected != null)
            {
                this.InvoiceCorrected.Invoke(new object(), new TaskUpdatedEventArgs(taskId, HttpContext.Current.ProviderUserKey().Value, instanceId));
            }
        }

        /// <summary>
        /// Raises the invoice confirmed event.
        /// </summary>
        /// <param name="taskId">The identifier of updated task.</param>
        /// <param name="instanceId">The workflow instance identifier for the workflow instance that contains the System.Workflow.Activities.HandleExternalEventActivity that is expected to handle the event.</param>
        public void RaiseInvoiceConfirmed(int taskId, Guid instanceId)
        {
            if (this.InvoiceConfirmed != null)
            {
                this.InvoiceConfirmed.Invoke(new object(), new TaskUpdatedEventArgs(taskId, HttpContext.Current.ProviderUserKey().Value, instanceId));
            }
        }

        /// <summary>
        /// Raises the invoice approved by chief accountant event.
        /// </summary>
        /// <param name="taskId">The identifier of updated task.</param>
        /// <param name="instanceId">The workflow instance identifier for the workflow instance that contains the System.Workflow.Activities.HandleExternalEventActivity that is expected to handle the event.</param>
        public void RaiseInvoiceApprovedByChiefAccountant(int taskId, Guid instanceId)
        {
            if (this.InvoiceApprovedByChiefAccountant != null)
            {
                this.InvoiceApprovedByChiefAccountant.Invoke(new object(), new TaskUpdatedEventArgs(taskId, HttpContext.Current.ProviderUserKey().Value, instanceId));
            }
        }

        /// <summary>
        /// Raises the invoice approved by CEO event.
        /// </summary>
        /// <param name="taskId">The identifier of updated task.</param>
        /// <param name="instanceId">The workflow instance identifier for the workflow instance that contains the System.Workflow.Activities.HandleExternalEventActivity that is expected to handle the event.</param>
        public void RaiseInvoiceApprovedByCEO(int taskId, Guid instanceId)
        {
            if (this.InvoiceApprovedByCEO != null)
            {
                this.InvoiceApprovedByCEO.Invoke(new object(), new TaskUpdatedEventArgs(taskId, HttpContext.Current.ProviderUserKey().Value, instanceId));
            }
        }

        /// <summary>
        /// Raises the invoice finally approved.
        /// </summary>
        /// <param name="taskId">The identifier of updated task.</param>
        /// <param name="instanceId">The workflow instance identifier for the workflow instance that contains the System.Workflow.Activities.HandleExternalEventActivity that is expected to handle the event.</param>
        public void RaiseInvoiceFinallyApproved(int taskId, Guid instanceId)
        {
            if (this.InvoiceFinallyApproved != null)
            {
                this.InvoiceFinallyApproved.Invoke(new object(), new TaskUpdatedEventArgs(taskId, HttpContext.Current.ProviderUserKey().Value, instanceId));
            }
        }

        /// <summary>
        /// Raises the invoice archived.
        /// </summary>
        /// <param name="taskId">The identifier of updated task.</param>
        /// <param name="instanceId">The workflow instance identifier for the workflow instance that contains the System.Workflow.Activities.HandleExternalEventActivity that is expected to handle the event.</param>
        public void RaiseInvoiceArchived(int taskId, Guid instanceId)
        {
            if (this.InvoiceArchived != null)
            {
                this.InvoiceArchived.Invoke(new object(), new TaskUpdatedEventArgs(taskId, HttpContext.Current.ProviderUserKey().Value, instanceId));
            }
        }
    }
}
